import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/hj_page/home_business/widget/user_avatar.dart';
import 'package:tiktokadult/hj_page/home_business/widget/user_name_view.dart';
import 'package:tiktokadult/hj_page/mine_business/ai/ai_home_menu_page.dart';
import 'package:tiktokadult/hj_page/mine_business/collec_history_buy/col_his_buy_page.dart';
import 'package:tiktokadult/hj_page/mine_business/mine_following/mine_following_view.dart';
import 'package:tiktokadult/hj_page/mine_business/mine_video_cache/video_cache_page.dart';
import 'package:tiktokadult/hj_page/mine_business/mine_vip/mine_charge_coin_page.dart';
import 'package:tiktokadult/hj_page/mine_business/welfare/welfare_home_page.dart';
import 'package:tiktokadult/hj_utils/holiday_theme.dart';
import 'package:tiktokadult/hj_utils/screen.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/global_store/store.dart';

import '../home_mine_logic.dart';
import '../mine_drive/mine_drive_page.dart';
import '../mine_post/mine_publish_mian_page.dart';
import '../mine_setting/mine_code_page.dart';
import '../mine_share/mine_share_page.dart';
import '../mine_utils.dart';

class MineInfoWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<MineMainLogic>(
      builder: (controller) {
        return Container(
          margin: EdgeInsets.only(left: 16, top: 12),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Container(
                color: Colors.transparent,
                child: Row(
                  children: [
                    UserAvatar(
                      size: 60,
                      showBorder: false,
                      model: Publisher()
                        ..portrait = controller.userInfo?.portrait
                        ..vipLevel = (controller.userInfo?.isVip == true) ? 1 : 0,
                    ),
                    12.sizeBoxW,
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Consumer<GlobalStore>(
                          builder: (context, store, child) {
                            int level = store.meInfo?.vipLevel ?? 0;
                            String vipTitle = '';
                            if (level == 1) vipTitle = 'VIP';
                            if (level == 2) vipTitle = 'SVIP';
                            if (level >= 3) vipTitle = '至尊SVIP';
                            return Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Row(
                                  children: [
                                    UserNameView(
                                      name: controller.userInfo?.name ?? '',
                                      fontSize: 20,
                                      fontWeight: FontWeight.w500,
                                      isVip: (controller.userInfo?.vipLevel ?? 0) > 0,
                                      nameColor: Colors.white,
                                    ),
                                    8.sizeBoxW,
                                    Image.asset(
                                      store.isVIP() ? 'is_vip.png'.mineImgPath : 'is_no_vip.png'.mineImgPath,
                                      width: 24,
                                    ),
                                    2.sizeBoxW,
                                    level > 0
                                        ? Container(
                                            alignment: Alignment.center,
                                            height: 20,
                                            padding: EdgeInsets.symmetric(horizontal: 6),
                                            decoration: BoxDecoration(
                                              color: Color(0xffF4CF9F),
                                              borderRadius: BorderRadius.circular(3),
                                            ),
                                            child: Text(
                                              vipTitle,
                                              style: textStyle(10, Color(0xff9D5D46), FontWeight.w400),
                                            ),
                                          )
                                        : Text(
                                            '',
                                            style: textStyle(10, Colors.white.withOpacity(.55), FontWeight.w400),
                                          )
                                  ],
                                ),
                                5.sizeBoxH,
                                store.isVIP()
                                    ? ShaderMask(
                                        shaderCallback: (Rect bounds) {
                                          return LinearGradient(
                                            colors: [Color(0xffE3B284), Color(0xffFEEEDE)],
                                            tileMode: TileMode.mirror,
                                          ).createShader(bounds);
                                        },
                                        blendMode: BlendMode.srcATop,
                                        child: Center(
                                          child: Text(
                                            '畅享无限观影',
                                            style: TextStyle(fontSize: 12, fontWeight: FontWeight.w700),
                                          ),
                                        ),
                                      )
                                    : Text("成为会员即可无限观影", style: TextStyle(color: Color(0xFF989898), fontSize: 12)),
                              ],
                            );
                          },
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              Spacer(),
              SizedBox(width: 4.w),
            ],
          ),
        );
      },
    );
  }
}

//充值/分享/代理赚钱
class MineTaskMenuWidget extends StatelessWidget {
  double get itemWidth => (screen.screenWidth - 8 * 2 - 20) / 3;
  double get scaleRate => itemWidth / 120;
  late final double height = itemWidth * 120 / 113;
  final double ratio = 104 / 70;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MineMainLogic>(
      builder: (controller) {
        return Container(
          margin: EdgeInsets.symmetric(horizontal: 16),
          width: double.infinity,
          child: Row(
            children: [
              Expanded(
                child: AspectRatio(
                  aspectRatio: ratio,
                  child: InkWell(
                    onTap: () => Get.to(MineChargeCoinPage()),
                    child: _buildItem(
                      "金币充值",
                      "金币：${(globalStore.getWallet()?.amount ?? 0) + (globalStore.getWallet()?.income ?? 0)}",
                      HolidayTheme.theme.coin,
                      10,
                      Color(0xFFFF9000),
                    ),
                  ),
                ),
              ),
              Expanded(
                child: AspectRatio(
                  aspectRatio: ratio,
                  child: InkWell(
                    onTap: () => Get.to(
                      () => AiHomeMenuPage(),
                      preventDuplicates: false,
                    ),
                    child: _buildItem(
                      "分享邀请",
                      "邀请领红包",
                      HolidayTheme.theme.aiEnter,
                      10,
                      Color(0xFFF05A57),
                    ),
                  ),
                ),
              ),
              Expanded(
                child: AspectRatio(
                  aspectRatio: ratio,
                  child: InkWell(
                    onTap: () => Get.to(WelfareHomePage(), preventDuplicates: false, arguments: 0),
                    child: _buildItem("代理推广", "全网最高分成", HolidayTheme.theme.agent, 10, Color(0xFF6975FF)),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _buildItem(String title, String subTitle, String imagePath, double left, Color titleColor) {
    return InkWell(
      child: Stack(
        children: [
          Positioned.fill(
            child: Image.asset(
              imagePath,
            ),
          ),
          Positioned(
            left: left,
            top: 10,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: TextStyle(fontSize: 14, color: Color(0xFFFFFFFF), fontWeight: FontWeight.w400),
                ),
                Text(
                  subTitle,
                  style: TextStyle(fontSize: 12, color: Color(0xFFFFBB00), fontWeight: FontWeight.w400),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}

//常用功能
class MineFunctionGridView extends StatelessWidget {
  MineFunctionGridView({super.key});
  final functionLabels = ['我的购买', '我的收藏', '我的订阅', '我的社区'];
  final functionImgs = ['ic_my_buy.webp', 'ic_my_collect.webp', 'ic_my_sub.webp', 'ic_my_community.webp'];
  final functions = [
    () => Get.to(ColHisBuyPage(PageType.buy)),
    () => Get.to(ColHisBuyPage(PageType.collect)),
    () => Get.to(() => VideoCachePage()),
    () => Get.to(() => MinePublishPostPage()),
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16),
      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 0),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        color: Color(0xFF22242E),
      ),
      child: Column(
        children: [
          Container(
            margin: EdgeInsets.symmetric(horizontal: 16),
            alignment: Alignment.centerLeft,
            child: Text("常用功能", style: TextStyle(color: Colors.white, fontSize: 14, fontWeight: FontWeight.bold)),
          ),
          5.sizeBoxH,
          Container(
            child: GridView.builder(
              physics: NeverScrollableScrollPhysics(),
              padding: EdgeInsets.zero,
              shrinkWrap: true,
              itemCount: functionLabels.length,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 4,
                crossAxisSpacing: 0,
                mainAxisSpacing: 0,
                childAspectRatio: 1,
              ),
              itemBuilder: (context, index) {
                String titleDesc = functionLabels[index];
                String imagePath = functionImgs[index];

                return InkWell(
                  onTap: functions[index],
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Image.asset(imagePath.mineImgPath, width: 36),
                      8.sizeBoxH,
                      Text(
                        titleDesc,
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 12,
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          )
        ],
      ),
    );
  }
}

//入驻合作
class MineCooperationGridView extends StatelessWidget {
  MineCooperationGridView({super.key});
  final functionLabels = ['原创入驻', '原味商城', '应用推荐'];
  final functionImgs = ['ic_original_product.webp', 'ic_original_sexy.webp', 'ic_recommend_app.webp'];
  final functions = [
    () => Get.to(ColHisBuyPage(PageType.collect)),
    () => Get.to(ColHisBuyPage(PageType.history)),
    () => Get.to(() => VideoCachePage()),
    () => Get.to(WelfareHomePage(), preventDuplicates: false, arguments: 2),
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16),
      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 0),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        color: Color(0xFF22242E),
      ),
      child: Column(
        children: [
          Container(
            margin: EdgeInsets.symmetric(horizontal: 16),
            alignment: Alignment.centerLeft,
            child: Text("入驻合作", style: TextStyle(color: Colors.white, fontSize: 14, fontWeight: FontWeight.bold)),
          ),
          5.sizeBoxH,
          Container(
            child: GridView.builder(
              physics: NeverScrollableScrollPhysics(),
              padding: EdgeInsets.zero,
              shrinkWrap: true,
              itemCount: functionLabels.length,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 4,
                crossAxisSpacing: 0,
                mainAxisSpacing: 0,
                childAspectRatio: 1,
              ),
              itemBuilder: (context, index) {
                String titleDesc = functionLabels[index];
                String imagePath = functionImgs[index];

                return InkWell(
                  onTap: functions[index],
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Image.asset(imagePath.mineImgPath, width: 36),
                      8.sizeBoxH,
                      Text(
                        titleDesc,
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 12,
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          )
        ],
      ),
    );
  }
}

//我的服务
class MineServiceGridView extends StatelessWidget {
  MineServiceGridView({super.key});
  final functionLabels = ['我的喜欢', '预览记录', '下载缓存', '我的关注', '在线客服', '官方群', '邀请码', '兑换码'];
  final functionImgs = [
    'ic_like.webp',
    'ic_view.webp',
    'ic_download.webp',
    'ic_follow.webp',
    'ic_service.webp',
    'ic_group.webp',
    'ic_invite.webp',
    'ic_edit.webp'
  ];
  final functions = [
    () => Get.to(ColHisBuyPage(PageType.like)),
    () => Get.to(ColHisBuyPage(PageType.history)),
    () => Get.to(() => VideoCachePage()),
    () => Get.to(() => MineFollowingPage()),
    () => MineUtils.goCSPage(),
    () => Get.to(MineDrivePage()),
    () => Get.to(() => MineSharePage()),
    () => Get.to(() => MineExchangeCodePage(type: 1)),
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16),
      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 0),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        color: Color(0xFF22242E),
      ),
      child: Column(
        children: [
          Container(
            margin: EdgeInsets.symmetric(horizontal: 16),
            alignment: Alignment.centerLeft,
            child: Text("我的服务", style: TextStyle(color: Colors.white, fontSize: 14, fontWeight: FontWeight.bold)),
          ),
          5.sizeBoxH,
          Container(
            child: GridView.builder(
              physics: NeverScrollableScrollPhysics(),
              padding: EdgeInsets.zero,
              shrinkWrap: true,
              itemCount: functionLabels.length,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 4,
                crossAxisSpacing: 0,
                mainAxisSpacing: 0,
                childAspectRatio: 1,
              ),
              itemBuilder: (context, index) {
                String titleDesc = functionLabels[index];
                String imagePath = functionImgs[index];

                return InkWell(
                  onTap: functions[index],
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Image.asset(imagePath.mineImgPath, width: 28),
                      8.sizeBoxH,
                      Text(
                        titleDesc,
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 12,
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          )
        ],
      ),
    );
  }
}
